/*
 * evhdl.c
 *
 *  Created on: Nov 15, 2013
 */

#include "evhdl.h"

EVHD_tstData EVHD_stData;


void EVHD_vSInit(void)
{
	uint8 u8i;

	for (u8i = 0; u8i < EV_NumberOfRecEvents; u8i++)
	{
		EVHD_stData.u8rEeventDescritpor[u8i] = 0x00;
	}
}

void EVHD_vRegister(EVHD_tenEvents enReceiveEvent)
{

	EVHD_stData.u8rEeventDescritpor[enReceiveEvent] |= EVHD__nRegisterMask;

}

void EVHD_vDeregister(EVHD_tenEvents enReceiveEvent)
{

	EVHD_stData.u8rEeventDescritpor[enReceiveEvent] &= (~EVHD__nRegisterMask);

}


void EVHD_vSendEvent(EVHD_tenEvents enSendEvent)
{
	if (EVHD__nRegisterMask
			== (EVHD__nRegisterMask
					& EVHD_stData.u8rEeventDescritpor[enSendEvent]))
	{
		/*Only if the event is Registered*/
		EVHD_stData.u8rEeventDescritpor[enSendEvent] |= EVHD__nEVActiveMask;
	}
	else
	{
		/*Do nothing */
	}
}


bool EVHD_boReadEvent(EVHD_tenEvents enReceiveEvent)
{
	bool bReturnEvent = FALSE;

	if (EVHD__nEVActiveMask
			== (EVHD__nEVActiveMask
					& EVHD_stData.u8rEeventDescritpor[enReceiveEvent]))
	{
		/*Only if the event has been sent*/
		bReturnEvent = TRUE;
		EVHD_stData.u8rEeventDescritpor[enReceiveEvent] &=
				(~EVHD__nEVActiveMask);
	}
	else
	{
		/* Do nothing */
	}

	return bReturnEvent;
}


void EVHD_vStartDispatcher(void)
{
	uint8 u8i;

	for (u8i = 0; u8i < EV_NumberOfRecEvents; u8i++)
	{
		if (EVHD__nRegisterMask
				== (EVHD__nRegisterMask & EVHD_stData.u8rEeventDescritpor[u8i]))
		{ /*Only if the event is Registered*/
			if (EVHD__nEVActiveMask
					== (EVHD__nEVActiveMask
							& EVHD_stData.u8rEeventDescritpor[u8i]))
			{/*Only if the event has been sent*/
				/*Clean the Event active Mask*/
				EVHD_stData.u8rEeventDescritpor[u8i] &= (~EVHD__nEVActiveMask);
				EVHD__apfReceiveEventFunc[u8i](); /*Execute the Event Function */
			}
			else
			{
				/*Do nothing*/
			}
		}
		else
		{
			/*Do nothing*/
		}
	}
}
